package com.qf.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.SysMenuDTO;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.vo.LayUIDTreeVO;
import com.qf.hospital.vo.R;
import com.qf.hospital.vo.SysMenuVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Description: ${TODO}
 * @Date 2021/10/19 14:52
 */

@RestController
public class MenuController {

    private Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private SysMenuService menuService;


    /**
     * 查询左侧的树结构
     * @param spread
     * @return
     */
    @PostMapping("/sel/loadMenuMangerLeftTreeJson")
    public R loadMenuMangerLeftTreeJson(Integer spread){
        // 将spread当做标识，如果传递了spread，只查询一级和二级菜单返回结果，应用于添加&修改
        //1. 调用Service查询菜单信息
        List<SysMenu> sysMenuList = menuService.findAll();
        //2. 封装数据
        List<LayUIDTreeVO> data = new ArrayList<>();
        if(spread != null){
            for (SysMenu sysMenu : sysMenuList) {
                if (sysMenu.getParentId() == 0 || sysMenu.getParentId() == 1) {
                    LayUIDTreeVO vo = new LayUIDTreeVO();
                    BeanUtils.copyProperties(sysMenu, vo);
                    data.add(vo);
                }
            }
        }else {
            for (SysMenu sysMenu : sysMenuList) {
                LayUIDTreeVO vo = new LayUIDTreeVO();
                BeanUtils.copyProperties(sysMenu, vo);
                data.add(vo);
            }
        }
        //3. 返回
        return R.ok(data);
    }

    /**
     * 查询右侧的表结构
     * @return
     */
    @GetMapping("/sel/queryMenuAllList")
    public R queryMenuAllList(String title,
                              @RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "10")Integer limit){

        PageInfo<SysMenu> pageInfo = menuService.findByTitle(title,page,limit);

        long count = pageInfo.getTotal();

        List<SysMenuVO> data = new ArrayList<>();

        for (SysMenu sysMenu : pageInfo.getList()) {
            SysMenuVO vo = new SysMenuVO();
            BeanUtils.copyProperties(sysMenu, vo);
            data.add(vo);
        }

        return R.ok(count, data);
    }


    /**
     * 查找有无子节点
     */
    @PostMapping("/sel/checkMenuHasChildren")
    public R checkMenuHasChildren(Integer id){
        return R.error(menuService.findParentId(id),"");
    }

    /**
     * 删除菜单
     */
    @PostMapping("/sel/deleteMenu")
    public R deleteMenu(Integer id){
        menuService.deleteById(id);
        return R.ok("删除成功！");
    }

    /**
     * 添加菜单
     */
    @PostMapping("/sel/addMenu")
    public R addMenu(@Valid SysMenuDTO sysMenuDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加菜单】 添加菜单 参数有误 ",msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR,msg);
        }

        SysMenu menu = new SysMenu();

        BeanUtils.copyProperties(sysMenuDTO, menu);

        menuService.addMenu(menu);
        return R.ok("添加菜单成功！");
    }

    /**
     * 编辑菜单
     */
    @PostMapping("/sel/updateMenu")
    public R updateMenu(@Valid SysMenuDTO sysMenuDTO,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【编辑菜单】 编辑菜单 参数有误！");
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR,msg);
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        menuService.updateMenu(sysMenu);
        return R.ok("修改成功！");
    }

//    pid: 1
//    pid_str: 医院管理系统
//    id: 2
//    title: 门诊管理
//    href:
//    icon: &#xe653;
//    spread: 0
//    available: 1
}
